package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/zw2582/ginlib"
	"net/http"
	"szxf/models"
	"szxf/services"
	"time"
)

//ResultIdName 身份证实名认证结果
type ResultIdName struct {
	Birth string     `json:"birth"`
	Sex   string     `json:"sex"`
	Addr  string     `json:"addr"`
}

func IdCheck(c *gin.Context)  {
	this := ginlib.Context{c}
	accountId := this.InputInt("account_id")
	name := this.InputStr("name")
	idCard := this.InputStr("id_card_no")
	sign := this.InputStr("sign")

	//验证ip
	if rc := models.ValidAccountIp(accountId, this.ClientIP()); rc != nil {
		this.JSON(http.StatusOK, gin.H{
			"code":rc,
			"uuid":ginlib.OrderNo(""),
			"msg":rc.Desc(),
		})
		return
	}

	//解析结果
	server := services.NewIdNameService(accountId, name, idCard, sign)
	rc := server.Check()
	
	//解析其他信息
	result := ResultIdName{}
	if rc == models.RC_ID_YIZHI {
		ginlib.RedisCache(fmt.Sprintf("otherinfo:idcard:%s", idCard), &result, time.Hour, func() (i interface{}) {
			result.Birth = services.GetBirth(idCard)
			result.Sex = services.GetSex(idCard)
			result.Addr = services.GetAddrByCode(idCard)
			return result
		})
	}

	this.JSON(http.StatusOK, gin.H{
		"uuid":server.LogReq.Uuid,
		"code":rc,
		"msg":rc.Desc(),
		"data":result,
	})
}
